package cn.itcast.xuesheng.web.servlet;

import cn.itcast.xuesheng.domain.Category;
import cn.itcast.xuesheng.service.CategoryService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import java.io.IOException;
import java.util.List;

//@WebServlet("/category/*")
@Controller
@RequestMapping("/category")
public class CategoryServlet extends BaseServlet {
    private ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");

    /**
     * 查询所有
     * @throws ServletException
     * @throws IOException
     */
    @RequestMapping("/findAll")
//    public void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       public @ResponseBody List findAll(){
        //1.调用service查询所有
        CategoryService service = (CategoryService) ac.getBean("categoryService");
        List<Category> cs = service.findall();
        //2.序列化json返回
       /* ObjectMapper mapper = new ObjectMapper();
        response.setContentType("application/json;charset=utf-8");
        mapper.writeValue(response.getOutputStream(),cs);*/
//        writeValue(response,cs);
        return cs;

    }

}
